package com.ckv.qcm.view.Utils;

import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

import org.jdesktop.swingx.VerticalLayout;

import com.ckv.qcm.view.Themes.ThemePanel;

public class ListPanel extends JPanel implements ElementPanelHandler {

	private ArrayList<ElementPanel> listElementPanels = new ArrayList<ElementPanel>();
	private JScrollPane scrollPane;
	private int elemWidth = 200;
	private int elemHeight = 50;
	private int height = 200;
	private ElementPanel selectedPane = null;
	private JPanel contentPane;
	private ThemePanel editPane = null;
	private boolean useEnteredColorChange = true;
	private boolean useSelectedColorChange = true;

	public ListPanel() {
		super();
		initComponent();
	}

	public ListPanel(int elemWidth, int elemHeight, int height) {
		super();
		System.out.println("List Height : " + height);
		if (elemWidth != -1)
			this.elemWidth = elemWidth;
		if (elemHeight != -1)
			this.elemHeight = elemHeight;
		this.height = height;
		initComponent();
	}

	public void addPanel(ElementPanel panel) {
		panel.addHandler(this);
		panel.setUseEnteredChangeColor(useEnteredColorChange);
		panel.setUseSelectedChangeColor(useSelectedColorChange);
		panel.setWidth(elemWidth - 4);
		panel.setHeight(elemHeight);
		this.contentPane.add(panel);
		listElementPanels.add(panel);
		this.validate();

		if (panel instanceof ThemePanel) {

			((ThemePanel) panel).focus();
			this.editPane = (ThemePanel) panel;

		}
		this.scrollPane.getVerticalScrollBar().setValue(
				this.scrollPane.getVerticalScrollBar().getMaximum());
	}

	public void removePanel(ElementPanel pane) {
		System.out.println("remove");
		pane.removeHandler(this);
		this.contentPane.remove(pane);
		listElementPanels.remove(pane);
		this.validate();
	}

	public void clear() {
		this.contentPane.removeAll();
		this.listElementPanels.clear();
		this.selectedPane = null;
		this.editPane = null;
	}

	private void initComponent() {
		this.setAlignmentX(CENTER_ALIGNMENT);
		// this.setPreferredSize(new Dimension(elemWidth,height));
		this.setBorder(BorderFactory.createLineBorder(Color.black));

		this.contentPane = new JPanel(new VerticalLayout());

		this.scrollPane = new JScrollPane(this.contentPane);
		this.scrollPane.setPreferredSize(new Dimension(elemWidth + 20,
				height - 20));
		this.scrollPane.getVerticalScrollBar().setUnitIncrement(elemHeight);

		this.scrollPane
				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		// this.add(this.contentPane);
		// this.scrollPane.add(this.contentPane);

		this.add(scrollPane);
	}

	public ElementPanel getSelectedPanel() {
		return selectedPane;
	}

	public ElementPanel getEditPanel() {
		return editPane;
	}

	@Override
	public void onSelected(Object source) {
		ElementPanel pane = (ElementPanel) source;

		if (((ElementPanel) pane).getMode() != ThemePanel.Mode.CREATE) {
			if (selectedPane != null) {
				if (selectedPane.isSelected()) {
					selectedPane.unselect();
				}
			}
			selectedPane = pane;
		}

	}
}
